import { BrowserWindow, Menu } from "electron";
import path from "path";
import { closeDevTools } from "./SwitchDevtools";
import { setWin } from "./initWin";
import url from "url";

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    show: false,
    webPreferences: {
      nodeIntegration: true,
      preload: path.join(__dirname, 'preload.js')
    },
  })
  win.setMinimumSize(580, 600);
  Menu.setApplicationMenu(Menu.buildFromTemplate([]));
  win.once('ready-to-show', () => {
    win.show()
  })
  setWin(win);
  // win.webContents.openDevTools()
  win.webContents.on('devtools-closed', closeDevTools);
  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
  } else {
    // 加载应用
    win.loadURL(url.format({
      pathname: path.join(__dirname, "index.html"),
      protocol: 'file',
      slashes: true,
      hash: '/'
    }))
  }
}

export default createWindow;